package org.example.express.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.express.service.MessageService;
import org.example.model.common.Result;
import org.example.model.common.ResultCodeEnum;
import org.example.model.domain.express.ExpressActivityMessage;
import org.example.model.domain.express.ExpressDispatchMessage;
import org.example.model.domain.express.ExpressSystemMessage;
import org.example.model.h5.LastMessageVo;
import org.example.model.h5.MVo;
import org.example.model.h5.MessageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping("/message")
@Slf4j
public class MessageController {
    @Autowired
    MessageService messageService;


    @RequestMapping("getMessage/{registerIp}")
    public Result getMessage(@PathVariable("registerIp") Long registerIp) {
        long l = System.currentTimeMillis();
        log.info("1获取消息耗时：{}", l);
        MVo mVo = new MVo();
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            MessageVo message = messageService.getMessage(registerIp);
            mVo.setMessageVo(message);
        });
        CompletableFuture<Void> voidCompletableFuture1 = CompletableFuture.runAsync(() -> {
            ArrayList<Integer> arrayList = messageService.countUnread(registerIp);
            mVo.setUnreadList(arrayList);
        });
        CompletableFuture.allOf(voidCompletableFuture, voidCompletableFuture1).join();
        log.info("1获取消息耗时：{}", System.currentTimeMillis() - l);
        return Result.build(mVo, ResultCodeEnum.SUCCESS);
    }

    /*获取最后一条消息*/
    @RequestMapping("getLastMessage/{registerIp}")
    public Result getLastMessage(@PathVariable("registerIp") Long registerIp) {
      LastMessageVo lastMessage = messageService.lastMessage(registerIp);
      return Result.build(lastMessage, ResultCodeEnum.SUCCESS);
    }
    /*获取未读信息统计*/
    @RequestMapping("countUnread/{registerIp}")
    public Result countUnread(@PathVariable("registerIp") Long registerIp) {
        return Result.build(messageService.countUnread(registerIp), ResultCodeEnum.SUCCESS);
    }

    @RequestMapping("getLastOneActivityMessage/{registerIp}")
    public Result getLastOneActivityMessage(@PathVariable("registerIp") Long registerIp) {
       ExpressActivityMessage expressActivityMessage = messageService.getLastOneActivityMessage(registerIp);
        return Result.build(expressActivityMessage, ResultCodeEnum.SUCCESS);
    }
    @RequestMapping("getLastOneSysMessage/{registerIp}")
    public Result getLastOneSysMessage(@PathVariable("registerIp") Long registerIp) {
     ExpressSystemMessage expressSystemMessage = messageService.getLastOneSysMessage(registerIp);
        return Result.build(expressSystemMessage, ResultCodeEnum.SUCCESS);
    }
    @RequestMapping("getLastOneDisMessage/{registerIp}")
    public Result getLastOneDisMessage(@PathVariable("registerIp") Long registerIp) {
        ExpressDispatchMessage expressDispatchMessage =messageService.getLastOneDisMessage(registerIp);
        return Result.build(expressDispatchMessage, ResultCodeEnum.SUCCESS);
    }




    @RequestMapping("getActivityMessage/{registerIp}")
    public Result getActivityMessage(@PathVariable("registerIp") Long registerIp) {
        List<ExpressActivityMessage> expressActivityMessage = messageService.getActivityMessage();
        return Result.build(expressActivityMessage, ResultCodeEnum.SUCCESS);
    }
    @RequestMapping("getSysMessage/{registerIp}")
    public Result getSysMessage(@PathVariable("registerIp") Long registerIp) {
        List<ExpressSystemMessage> expressSystemMessage = messageService.getSysMessage(registerIp);
        return Result.build(expressSystemMessage, ResultCodeEnum.SUCCESS);
    }
    @RequestMapping("getDisMessage/{registerIp}")
    public Result getDisMessage(@PathVariable("registerIp") Long registerIp) {
        List<ExpressDispatchMessage> expressDispatchMessage =messageService.getLastMessage(registerIp);
        return Result.build(expressDispatchMessage, ResultCodeEnum.SUCCESS);
    }








    /*获取Redis中信息*/
    @RequestMapping("getRedis")
    public Result updateRedis() {
        return messageService.getRedis();
    }


    /*修改系统消息为阅读*/
    @RequestMapping("updateSysMessage/{registerIp}")
    public Result updateSysMessage(@PathVariable("registerIp") Long registerIp) {
        return messageService.updateSysMessage(registerIp);
    }

    /*修改派件信息为阅读*/
    @RequestMapping("updateDispatchMessage/{id}")
    public Result updateDispatchMessage(@PathVariable("id") Integer id) {
        return messageService.updateDispatchMessage(id);
    }

    /*修改活动通知为阅读*/
    @RequestMapping("updateActivityMessage")
    public Result updateActivityMessage() {
        return messageService.updateActivityMessage();
    }
}
